package es.crawler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;

public class Crawler extends Thread{
	
	private String url;
	
	public Crawler(String url){
		super(url);
		if(url.endsWith("/"))
			url=url.substring(0, url.length()-1);
		this.url=url;
	}
	
	public void run(){
		URL init_url=null;
		try {
			init_url = new URL(url);
		} catch (MalformedURLException e) {
			System.out.println("Malformed URL: "+url);
		}
		if(init_url!=null) {
	        BufferedReader br = null;
	        try {
				br = new BufferedReader(new InputStreamReader(init_url.openStream()));
	        } catch (IOException e) {
				System.out.println("Error opening the stream: "+url);
				//e.printStackTrace();
			} 
	        
	        if(br!=null) {
		        String content = "";
		        String strTemp = "";
		        try  {
		        	while(null != (strTemp = br.readLine()))
		        		content+=strTemp;
	        	} catch (IOException e) {
	        		System.out.println("Error reading the line");
	        	} 
		        //looking for links
		        if(!content.isEmpty()) {
			        int start=content.indexOf("<a href=")+8;
			        while(start!=7 && Main.count<Main.MAX_URLS){
			            String quote = content.substring(start++,start); 
			        	int end=content.indexOf(quote, start);
			        	String link=content.substring(start, end);
			        	//check if link is relative
			        	if(!link.startsWith("http://") && !link.startsWith("https://")) {
			        		if(!link.startsWith("/")) 
			        			link=url+"/"+link;  
			        		else 
			        			link=url+link;
			        	}
			        				          		            	
			        	if(Main.count<Main.MAX_URLS){
			        		Crawler crawler=new Crawler(link);
			            	//System.out.println("Crawling into "+link);
			            	synchronized (Main.count) {
			            		Main.count++;
							}
			        		crawler.start();
			        	}
			        	start=content.indexOf("<a href=",end)+8;
			        }
		        }
	        }
		}
	}
}
